#!/bin/bash
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

#
# Copyright 2020 Joyent, Inc.
#

#
# This script is run as part of the biweekly SmartOS release process. It
# uploads the release artifacts from the platform build bits directory to
# ~~/public/SmartOS/<timestamp>/ , for processing
# by the https://github.com/joyent/smartos-changelog generator, and uploads
# the 'latest' release artifacts to ~~/public/SmartOS/*-latest.* to preserve
# compatibility with scripts that expect to find them there.
#

# Update the SmartOS release directory
if [ "$TRACE" != "" ]; then
    export PS4='${BASH_SOURCE}:${LINENO}: '
    set -o xtrace
fi

TOP=$(cd $(dirname $0)/../; pwd)
PATH=$PATH:${TOP}/node_modules/manta/bin

# --- Manta config

if [[ -z "$MANTA_KEY_ID" ]]; then
    export MANTA_KEY_ID=`ssh-keygen -l -f ~/.ssh/id_rsa.pub | awk '{print $2}' | tr -d '\n'`
fi
if [[ -z "$MANTA_URL" ]]; then
    export MANTA_URL=https://us-east.manta.joyent.com
fi
if [[ -z "$MANTA_USER" ]]; then
    export MANTA_USER="Joyent_Dev";
fi

# --- support functions

function fatal {
    echo "$(basename $0): error: $1"
    exit 1
}

function errexit {
    [[ $1 -ne 0 ]] || exit 0
    fatal "error exit status $1 at line $2"
}

function print_help() {
    echo "Usage:"
    echo "  ./tools/smartos-release BRANCH TIMESTAMP"
    echo ""
    echo "Upload build artifacts to /\${MANTA_USER}/public/SmartOS/\${TIMESTAMP}"
    echo "duplicating content from /\${MANTA_USER}/public/builds/platform/\${BRANCH}-\${TIMESTAMP}/platform/"
    echo "and uploading *-latest.* files to /\${MANTA_USER}/public/SmartOS/"
}

# --- mainline

trap 'errexit $? $LINENO' EXIT

BRANCH=$1
shift
TIMESTAMP=$1

if [[ -z "$BRANCH" ]] || [[ -z "$TIMESTAMP" ]]; then
    echo "Error: Missing branch and/or timestamp arguments"
    print_help
    exit 2
fi

set -o errexit

SMARTOS=/${MANTA_USER}/public/SmartOS
SMARTOS_RELEASE=${SMARTOS}/${TIMESTAMP}
BITS=output/bits/platform

start_time=$(date +%s)

echo "Uploading release objects to ${SMARTOS_RELEASE}"
mmkdir -p ${SMARTOS_RELEASE}

BUILD_FILES="platform-${BRANCH}-${TIMESTAMP}.tgz
    tests-${BRANCH}-${TIMESTAMP}.tgz
    smartos-${TIMESTAMP}.iso
    smartos-${TIMESTAMP}-USB.img.gz
    smartos-${TIMESTAMP}.vmwarevm.tar.gz
    SINGLE_USER_ROOT_PASSWORD.txt
    build.log
    changelog.txt
    configure-projects
    gitstatus.json
    index.html
    md5sums.txt"
for file in ${BUILD_FILES}; do
    mput -f ${BITS}/$file ${SMARTOS_RELEASE}/$file
done

echo "Uploading 'latest' ${SMARTOS} objects to ${SMARTOS}"
mput -f $BITS/platform-${BRANCH}-${TIMESTAMP}.tgz ${SMARTOS}/platform-latest.tgz
mput -f $BITS/smartos-${TIMESTAMP}.iso ${SMARTOS}/smartos-latest.iso
mput -f $BITS/smartos-${TIMESTAMP}-USB.img.gz ${SMARTOS}/smartos-latest-USB.img.gz
mput -f $BITS/smartos-${TIMESTAMP}.vmwarevm.tar.gz ${SMARTOS}/smartos-latest.vmwarevm.tar.gz

echo "Updating ${SMARTOS}/latest object"
echo ${SMARTOS_RELEASE} | mput -H 'content-type: text/plain' ${SMARTOS}/latest

# The index.html file referenced here gets created by 'smartos-index'
echo "<html><head><meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=${SMARTOS_RELEASE}/index.html\"></head></html>" | mput -H 'content-type: text/html' ${SMARTOS}/latest.html

end_time=$(date +%s)
elapsed=$((${end_time} - ${start_time}))
echo "Uploading SmartOS release bits took ${elapsed} seconds (Manta path=${SMARTOS_RELEASE})"
exit 0
